package com.example.async.service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class TaskService {

	@Autowired
	private ThreadPoolTaskExecutor executor;

	public void executeAsyncNonBlocking() {
		System.out.println("----------执行异步任务（非阻塞），开始----------");

		long start = System.currentTimeMillis();
		CompletableFuture<Void> task1Future = CompletableFuture.runAsync(() -> {
			task1();
		}, executor);

		CompletableFuture<Void> task2Future = CompletableFuture.runAsync(() -> {
			task2();
		}, executor);

		CompletableFuture<Void> task3Future = CompletableFuture.runAsync(() -> {
			task3();
		}, executor);

		CompletableFuture.allOf(task1Future, task2Future, task3Future).thenRun(() -> {
			System.out.println("----------执行所有异步任务，结束----------");
			System.out.println("执行时间：" + (System.currentTimeMillis() - start) + " 毫秒");
		});

		System.out.println("----------执行异步任务（主线程），结束----------");
		System.out.println("执行时间：" + (System.currentTimeMillis() - start) + " 毫秒");
	}

	/**
	 * 执行异步任务
	 * 
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public void executeAsync() throws InterruptedException, ExecutionException {
		System.out.println("----------执行异步任务，开始----------");

		long start = System.currentTimeMillis();
		CompletableFuture<Void> task1Future = CompletableFuture.runAsync(() -> {
			task1();
		}, executor);

		CompletableFuture<Void> task2Future = CompletableFuture.runAsync(() -> {
			task2();
		}, executor);

		CompletableFuture<Void> task3Future = CompletableFuture.runAsync(() -> {
			task3();
		}, executor);

		CompletableFuture.allOf(task1Future, task2Future, task3Future).get();

		long end = System.currentTimeMillis();

		System.out.println("----------执行异步任务，结束----------");
		System.out.println("执行时间：" + (end - start) + " 毫秒");
	}

	/**
	 * 执行同步任务
	 */
	public void executeSync() {
		System.out.println("----------执行同步任务，开始----------");

		long start = System.currentTimeMillis();
		task1();
		task2();
		task3();
		long end = System.currentTimeMillis();

		System.out.println("----------执行同步任务，结束----------");
		System.out.println("执行时间：" + (end - start) + " 毫秒");
	}

	private void task1() {
		try {
			System.out.println("task1 开始");
			long start = System.currentTimeMillis();
			Thread.sleep(1000);
			long end = System.currentTimeMillis();
			System.out.println("task1 结束，执行时间：" + (end - start) + " 毫秒");
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	private void task2() {
		try {
			System.out.println("task2 开始");
			long start = System.currentTimeMillis();
			Thread.sleep(2000);
			long end = System.currentTimeMillis();
			System.out.println("task2 结束，执行时间：" + (end - start) + " 毫秒");
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	private void task3() {
		try {
			System.out.println("task3 开始");
			long start = System.currentTimeMillis();
			Thread.sleep(3000);
			long end = System.currentTimeMillis();
			System.out.println("task3 结束，执行时间：" + (end - start) + " 毫秒");
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

}
